package com.lfs.feishu.controller;

import com.google.gson.JsonParser;
import com.lark.oapi.Client;
import com.lark.oapi.core.request.RequestOptions;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.calendar.v4.model.*;
import com.lfs.feishu.service.UserInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

import static com.lfs.feishu.constant.AppInfo.APP_ID;
import static com.lfs.feishu.constant.AppInfo.APP_SECRET;

@RequestMapping("/feishu/calendar")
@RestController
@AllArgsConstructor
@Slf4j
public class CalendarController {
    UserInfoService userInfoService;


    /**
     * 创建日历
     */
    @RequestMapping("/create")
    public String createCalendar() throws Exception {
        // 构建client
        Client client = Client.newBuilder(APP_ID, APP_SECRET).build();

        // 创建请求对象
        CreateCalendarReq req = CreateCalendarReq.newBuilder()
                .calendar(Calendar.newBuilder()
                        .summary("天行平台日历")
                        .description("天行平台创建的日历")
                        .permissions("private")
                        .color(-1)
                        .summaryAlias("日历备注名")
                        .build())
                .build();

        // 发起请求
        CreateCalendarResp resp = client.calendar().v4().calendar().create(req);

        // 处理服务端错误
        if (!resp.success()) {
            log.info(String.format("code:%s,msg:%s,reqId:%s, resp:%s", resp.getCode(), resp.getMsg(), resp.getRequestId(), Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8)))));
        }

        // 业务数据处理
        log.info("创建日历结果：{}",Jsons.DEFAULT.toJson(resp.getData()));
        return resp.getData().getCalendar().getCalendarId();
    }


    /**
     * 查询主日历（每次调用前查询）
     */
    @GetMapping("/mainCalendars")
    public UserCalendar[] getCalendars() throws Exception {
        // 构建client
        Client client = Client.newBuilder(APP_ID, APP_SECRET).build();

        // 创建请求对象
        PrimaryCalendarReq req = PrimaryCalendarReq.newBuilder()
                .userIdType("open_id")
                .build();

        // 发起请求
        PrimaryCalendarResp resp = client.calendar().v4().calendar().primary(req);

        // 处理服务端错误
        if (!resp.success()) {
            System.out.println(String.format("code:%s,msg:%s,reqId:%s, resp:%s",
                    resp.getCode(), resp.getMsg(), resp.getRequestId(), Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8)))));
        }

        // 业务数据处理
        return resp.getData().getCalendars();
    }


    /**
     * 查询日历列表
     */
    @GetMapping("/calendarList")
    public Calendar[] calendarList() throws Exception {
        // 构建client
        Client client = Client.newBuilder(APP_ID, APP_SECRET).build();

        // 创建请求对象
        ListCalendarReq req = ListCalendarReq.newBuilder()
                .pageSize(1000)
//                .pageToken("ListCalendarsPageToken_xxx")
//                .syncToken("ListCalendarsSyncToken_xxx")
                .build();

        // 发起请求
        ListCalendarResp resp = client.calendar().v4().calendar().list(req);

        // 处理服务端错误
        if (!resp.success()) {
            System.out.println(String.format("code:%s,msg:%s,reqId:%s, resp:%s",
                    resp.getCode(), resp.getMsg(), resp.getRequestId(), Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8)))));
        }
        return resp.getData().getCalendarList();
    }
}
